package com.sugon.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.RandomUtil;
import com.sugon.dao.ModuleArchivesDao;
import com.sugon.dao.ModuleErrorDao;
import com.sugon.dao.ModuleMaintainPlanDao;
import com.sugon.dao.SysUserDao;
import com.sugon.entity.ModuleArchivesEntity;
import com.sugon.entity.ModuleErrorEntity;
import com.sugon.entity.ModuleMaintainPlanEntity;
import com.sugon.entity.SysMsgEntity;
import com.sugon.modules.mold.consts.MoldPmTicketProgressConst;
import com.sugon.modules.mold.consts.MoldReplacePartProgress;
import com.sugon.modules.mold.service.IMoldArchivesManager;
import com.sugon.service.ModuleMaintainPlanService;
import com.sugon.service.SysMsgService;
import com.sugon.utils.Constant;
import com.sugon.utils.ProcessInfoEnum;
import com.sugon.utils.ShiroUtils;
import com.sugon.utils.SysMsgType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.sugon.utils.ShiroUtils.getUserId;

/**
 * 模具维护计划Service实现类
 *
 * @author sugon
 * @date 2019-08-19 09:25:11
 */
@Service("moduleMaintainPlanService")
public class ModuleMaintainPlanServiceImpl implements ModuleMaintainPlanService {
    @Autowired
    private ModuleMaintainPlanDao moduleMaintainPlanDao;
    @Autowired
    private ModuleErrorDao moduleErrorDao;
    @Autowired
    private SysMsgService sysMsgService;
    @Autowired
    private ModuleArchivesDao moduleArchivesDao;
    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private IMoldArchivesManager moldArchivesManager;

    /**
     * queryByCode
     *
     * @param replaceCode 维护工单号
     * @return
     */
    @Override
    public ModuleMaintainPlanEntity queryByCode(String replaceCode) {
        Map<String, Object> queryParam = MapUtil.newHashMap(1);
        queryParam.put("planName", replaceCode);
        List<ModuleMaintainPlanEntity> maintenanceTicketList = queryList(queryParam);
        return CollectionUtil.isNotEmpty(maintenanceTicketList) ? maintenanceTicketList.get(0) : null;
    }

    /**
     * queryObject
     *
     * @param id 主键
     * @return
     */
    @Override
    public ModuleMaintainPlanEntity queryObject(String id) {
        return moduleMaintainPlanDao.queryObject(id);
    }

    /**
     * queryList
     *
     * @param map 参数
     * @return
     */
    @Override
    public List<ModuleMaintainPlanEntity> queryList(Map<String, Object> map) {
        return moduleMaintainPlanDao.queryList(map);
    }

    /**
     * queryList2
     *
     * @param map
     * @return
     */
    @Override
    public List<ModuleMaintainPlanEntity> queryList2(Map<String, Object> map) {
        return moduleMaintainPlanDao.queryList(map);
    }

    /**
     * queryTotal
     *
     * @param map 参数
     * @return
     */
    @Override
    public int queryTotal(Map<String, Object> map) {
        return moduleMaintainPlanDao.queryTotal(map);
    }

    /**
     * queryTotal2
     *
     * @param map
     * @return
     */
    @Override
    public int queryTotal2(Map<String, Object> map) {
        return moduleMaintainPlanDao.queryTotal(map);
    }

    /**
     * save
     *
     * @param moduleMaintainPlan 实体
     * @return
     */
    @Override
    public int save(ModuleMaintainPlanEntity moduleMaintainPlan) {
        moduleMaintainPlan.setId(RandomUtil.randomString(32));
        moduleMaintainPlan.setDelFlag(Integer.valueOf(Constant.IS_NOT_DEL));
        moduleMaintainPlan.setCreateDate(new Date());
        moduleMaintainPlan.setCreateUser(getUserId());
        moduleMaintainPlan.setIsPutOff(0);
        moduleMaintainPlan.setIsSure(0);
        moduleMaintainPlan.setProgress(MoldPmTicketProgressConst.NEW);
        moduleMaintainPlan.setReplacePartProgress(MoldReplacePartProgress.NEW);
        //添加车间
        String moldId = moduleMaintainPlan.getMoldId();
        ModuleArchivesEntity module = moduleArchivesDao.queryObject(moldId);
        moduleMaintainPlan.setDeptCode(module.getSubShop());
        moduleMaintainPlan.setMoldId(module.getId());
        return moduleMaintainPlanDao.save(moduleMaintainPlan);
    }

    /**
     * update
     *
     * @param moduleMaintainPlan 实体
     * @return
     */
    @Override
    public int update(ModuleMaintainPlanEntity moduleMaintainPlan) {
        moduleMaintainPlan.setUpdateDate(DateUtil.date());
        moduleMaintainPlan.setUpdateUser(ShiroUtils.getUserId());
        return moduleMaintainPlanDao.update(moduleMaintainPlan);
    }

    /**
     * delete
     *
     * @param id
     * @return
     */
    @Override
    public int delete(String id) {
        return moduleMaintainPlanDao.delete(id);
    }

    /**
     * deleteBatch
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(String[] ids) {
        return moduleMaintainPlanDao.deleteBatch(ids);
    }

    /**
     * getFixer
     *
     * @return
     */
    @Override
    public List getFixer() {
        // 获取同一个部门下的维修人员列表
        Map<String, Object> map = new HashMap<>();
        map.put("deptCode", ShiroUtils.getDeptId());
        return sysUserDao.loadFixer(map);
    }

    /**
     * updateBatch
     *
     * @param ids
     * @return
     */
    @Override
    public int updateBatch(List<String> ids) {
        return moduleMaintainPlanDao.updateBatch(ids);
    }

    /**
     * isOutTime
     *
     * @param id
     * @return
     */
    @Override
    public int isOutTime(String id) {
        return moduleMaintainPlanDao.isOutTime(id);
    }

    /**
     * endProcess
     *
     * @param moduleMaintainPlan
     * @return
     */
    @Override
    public boolean endProcess(ModuleMaintainPlanEntity moduleMaintainPlan) {
        if (update(moduleMaintainPlan) > 0) {
            // 发送流程结束消息
            saveSysMsgEntity(moduleMaintainPlan.getId(), 0);
            String moldId = moduleMaintainPlan.getMoldId();
            // 恢复模具状态到维护前
            ModuleArchivesEntity moduleArchives = moduleArchivesDao.queryObject(moldId);
            cn.hutool.core.lang.Assert.notNull(moduleArchives, "模具[{}]不存在，请确认！", moldId);
            moldArchivesManager.recoverStatusByMoldId(moduleArchives.getId());
        }
        return true;
    }

    /**
     * isSure
     *
     * @param moduleMaintainPlan
     * @return
     */
    @Override
    public int isSure(ModuleMaintainPlanEntity moduleMaintainPlan) {
        saveSysMsgEntity(moduleMaintainPlan.getId(), 2);
        return moduleMaintainPlanDao.isSure(moduleMaintainPlan);
    }

    /**
     * dealError
     *
     * @param param
     * @return
     */
    @Override
    public int dealError(Map<String, Object> param) {
        String id = String.valueOf(param.get("id"));
        ModuleMaintainPlanEntity mmpr = moduleMaintainPlanDao.queryObject(id);
        String workShop = mmpr.getDeptCode();
        String modCode = mmpr.getModuleCode();
        //查询模具信息
//        Map map =  moduleMaintainPlanDao.getModuleBase(id);
        //根据id查询产品系列 等信息
        ModuleErrorEntity errorEntity = new ModuleErrorEntity();
        errorEntity.setId(RandomUtil.randomString(32));
        errorEntity.setCreateTime(new Date());
        errorEntity.setCreateUser(ShiroUtils.getUserId());
        errorEntity.setIsOff(Integer.valueOf(String.valueOf(param.get("isOff"))));
        errorEntity.setIsReplaceSparepart(Integer.valueOf(String.valueOf(param.get("isReplaceSparepart"))));
        errorEntity.setDealOpinion(String.valueOf(param.get("dealOpinion")));
        errorEntity.setErrorDisStatus(String.valueOf(param.get("errorDisStatus")));
        errorEntity.setDealSuggest(String.valueOf(param.get("dealSuggest")));
        errorEntity.setDelFlag(1);
        errorEntity.setMaintainRecord(String.valueOf(param.get("id")));//所属维护记录
        errorEntity.setMaintainTime(new Date());
        errorEntity.setWorkshop(workShop);
        errorEntity.setModuleCode(modCode);

        ModuleArchivesEntity md = moduleArchivesDao.queryObjectByCode(modCode);
        if (md != null) {
            errorEntity.setModuleType(md.getModuleType());
            errorEntity.setProductSeries(md.getModuleSeries());
        }


//        errorEntity.setWorkshop(String.valueOf(map.get("workShop")));
//        errorEntity.setProductLine(String.valueOf(map.get("productLine")));
//        errorEntity.setWorkshop(String.valueOf(param.get("workShop")));
//        errorEntity.setProductLine(String.valueOf(param.get("productLine")));
//        errorEntity.setModuleType(String.valueOf(map.get("")));
//        errorEntity.setProductSeries(String.valueOf(map.get("")));
//        errorEntity
        moduleErrorDao.save(errorEntity);
        ModuleMaintainPlanEntity entity = new ModuleMaintainPlanEntity();
        entity.setId(id);
        return moduleMaintainPlanDao.endProcess(entity);
    }

    /**
     * hasError
     *
     * @param id
     * @return
     */
    @Override
    public int hasError(String id) {
        return moduleErrorDao.getInfoByRecord(id);
    }

    /**
     * saveSysMsgEntity
     *
     * @param Id
     * @param status
     */
    public void saveSysMsgEntity(String Id, Integer status) {
        SysMsgEntity sysMsgEntity = new SysMsgEntity();
        if (2 == status) {
            //审核不通过，消息流程提醒结束
            sysMsgEntity.setDataId(Id);
            sysMsgEntity.setDataType(ProcessInfoEnum.MDE_DQWH);
            sysMsgEntity.setDataFlag(SysMsgType.DATA_DEL_END);
        } else {
            sysMsgEntity.setDataId(Id);
            sysMsgEntity.setDataUrl("../module/modulemaintenplan.html");
            sysMsgService.addApproveItem(ProcessInfoEnum.MDE_DQWH, sysMsgEntity, 0);
        }
        sysMsgService.allMsgData(sysMsgEntity);
    }
}
